﻿using System;
using System.Diagnostics;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet
{
    partial class WorksheetPane
    {
        private WorksheetPaneDisabler _disabler;

        class WorksheetPaneDisabler : IDisposable
        {
            private readonly WorksheetPane _pane;
            private bool _needsRecompute;
            private bool _forceRowColumns;
            private bool _layoutChangedCalled;

            public WorksheetPaneDisabler(WorksheetPane pane)
            {
             //   Debug.WriteLine("Disabling " + pane.Worksheet.Name +  pane.GetHashCode());
                _pane = pane;
            }
            public void Recompute(bool forceRowColumns)
            {
                _needsRecompute = true;
                _forceRowColumns = forceRowColumns;
            }
            public void Dispose()
            {
                Contract.Assert(_pane._disabler == this);
                _pane._disabler = null;
                if (_layoutChangedCalled)
                {
                    _pane.OnLayoutChanged();

                }
                if (_needsRecompute)
                    _pane.recompute(_forceRowColumns);
            }

            public void LayoutChanged()
            {
                this._layoutChangedCalled = true;
            }
        }
        internal IDisposable CreateDisabler ()
        {
            Contract.Assert(_disabler == null);
            _disabler = new WorksheetPaneDisabler(this);
            return _disabler;
        }
    }
}